<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel = "stylesheet" href = "css/toastr.css">
<title>添加商品</title>
</head>
<body>
	<form action="#">
		商品名称：<input type = "text" name = "goodsName" id = "goods_name"><br>
		商品类型：<input type = "text" name = "goodsType" id = "goods_type"><br>
		商品价格：<input type = "text" name = "goodsPrice" id = "goods_price"><br>
		商品数量：<input type = "text" name = "goodsCount" id = "goods_count"><br>
		<button type="button" id = "btn">添加商品</button>
	</form>
</body>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/toastr.js"></script>
<script type="text/javascript">
	$("#btn").click(function () {
		
		//获取数据
		var goods_name = $("#goods_name").val();
		var goods_type = $("#goods_type").val();
		var goods_price = $("#goods_price").val();
		var goods_count = $("#goods_count").val();
		
		//声明正则表达式
		var reg_name = /^[a-zA-Z0-9\u4E00-\u9FA5]{2,6}$/;
		var reg_type = /^[a-zA-Z\u4E00-\u9FA5]{2,6}$/;
		var reg_price = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
		var reg_count = /^([1-9][0-9]*)$/;
		//判断
		if(!reg_name.test(goods_name)){
			toastr.warning("请填写正确的商品名");
		}else if(!reg_type.test(goods_type)){
			toastr.warning("请填写正确的类型");
		}else if(!reg_price.test(goods_price)){
			toastr.warning("请填写正确的价格");
		}else if(!reg_count.test(goods_count)){
			toastr.warning("请填写正整数");
		}else{
			
			//验证是否已存在
			validataName(goods_name,goods_type,goods_price,goods_count);
		}
	})
	function validataName(goods_name,goods_type,goods_price,goods_count) {
		$.ajax({
			url:"addGoods.do",
			type:"POST",
			data:{"goodsName":goods_name,"goodsType":goods_type,
				"goodsPrice":goods_price,"goodsCount":goods_count},
			success:function(data){
				toastr.info(data);
				window.location.href = "queryAll.do";
			},
			error:function(){
				toastr.error("错误，异常");
			}
		})
	}
</script>
</html>